using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class TournamentCareerStats
{
	public enum EventScore
	{
		NeverPlayed = 0,
		NotRanked = 6,
		Quarterfinals = 7,
		Semifinals = 8,
		Finals = 9,
		Won = 10
	}

	private static string tournamentStatsFileName = "tournamentCareerStats.bytes";

	private TournamentCareerDAO tournamentCareer;

	public TournamentCareerDAO TournamentCareer
	{
		get
		{
			return tournamentCareer;
		}
	}

	public TournamentCareerStats()
	{
		ReloadModel();
	}

	public void ReloadModel()
	{
		if (!ProtoIO.LoadFromFile<ProtoSerializer, TournamentCareerDAO>(tournamentStatsFileName, GGFileIO.instance, out tournamentCareer))
		{
			Debug.Log("creating TournamentCareerDAO");
			tournamentCareer = GetTournamentCareerStart();
			ProtoIO.SaveToFile<ProtoSerializer, TournamentCareerDAO>(tournamentStatsFileName, GGFileIO.instance, tournamentCareer);
		}
		if (tournamentCareer.genericTournamentStats == null)
		{
			tournamentCareer.genericTournamentStats = new List<TournamentCareerDAO.GenericEventStats>();
		}
		if (tournamentCareer.namedTournamentPerformances == null)
		{
			tournamentCareer.namedTournamentPerformances = new List<TournamentCareerDAO.NamedEventPerformance>();
		}
	}

	private TournamentCareerDAO GetTournamentCareerStart()
	{
		SeasonDefinitions.Season season = SeasonDefinitions.instance.seasons[0];
		SeasonDefinitions.SeasonInterval interval = SeasonDefinitions.instance.seasons[0].seasonIntervals[0];
		TournamentCareerDAO tournamentCareerDAO = new TournamentCareerDAO();
		tournamentCareerDAO.activeSeasonInterval = GetNewSeasonIntervalInfo(season, interval);
		return tournamentCareerDAO;
	}

	public void BeginSeasonInterval(SeasonDefinitions.Season season, SeasonDefinitions.SeasonInterval interval)
	{
		tournamentCareer.activeSeasonInterval = GetNewSeasonIntervalInfo(season, interval);
		Save();
	}

	public void AdvanceToNextSeason()
	{
		SeasonDefinitions.SeasonId seasonId = (SeasonDefinitions.SeasonId)TournamentCareer.activeSeasonInterval.seasonId;
		SeasonDefinitions.SeasonIntervalId seasonIntervalId = (SeasonDefinitions.SeasonIntervalId)TournamentCareer.activeSeasonInterval.seasonIntervalId;
		SeasonDefinitions.NextIntervalInfo infoForNextInterval = SeasonDefinitions.instance.GetInfoForNextInterval(seasonId, seasonIntervalId);
		BeginSeasonInterval(infoForNextInterval.season, infoForNextInterval.season.seasonIntervals[0]);
	}

	public SeasonDefinitions.SeasonIntervalId GetActiveIntervalId()
	{
		return (SeasonDefinitions.SeasonIntervalId)TournamentCareer.activeSeasonInterval.seasonIntervalId;
	}

	private TournamentCareerDAO.ActiveSeasonInterval GetNewSeasonIntervalInfo(SeasonDefinitions.Season season, SeasonDefinitions.SeasonInterval interval)
	{
		TournamentCareerDAO.ActiveSeasonInterval activeSeasonInterval = new TournamentCareerDAO.ActiveSeasonInterval();
		activeSeasonInterval.seasonId = (int)season.seasonId;
		activeSeasonInterval.seasonIntervalId = (int)interval.intervalId;
		activeSeasonInterval.currentEvents = new List<TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent>();
		foreach (SeasonDefinitions.SeasonEvent item2 in interval.eventsForInterval)
		{
			TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent item = CreateNewEventEntryForType(item2.namedEventId);
			activeSeasonInterval.currentEvents.Add(item);
		}
		return activeSeasonInterval;
	}

	private TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent CreateNewEventEntryForType(SeasonDefinitions.NamedEventId eventId)
	{
		TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent activeEvent = new TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent();
		activeEvent.stageIndex = 0;
		activeEvent.isStarted = false;
		activeEvent.isFinished = false;
		activeEvent.eventInfo = new TournamentCareerDAO.NamedEventPerformance();
		activeEvent.eventInfo.namedEventId = (int)eventId;
		activeEvent.eventInfo.score = 0;
		return activeEvent;
	}

	public void ReportGameStarted(SeasonDefinitions.SeasonEvent seasonEvent)
	{
		GetGenericEventStats(seasonEvent.eventDefinitionId).timesPlayed++;
		Save();
	}

	public void AdvanceActiveEvent(SeasonDefinitions.SeasonEvent seasonEvent, EventScore score, bool isWin)
	{
		TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent activeEvent = GetActiveEvent(seasonEvent.namedEventId);
		if (activeEvent == null)
		{
			activeEvent = CreateNewEventEntryForType(seasonEvent.namedEventId);
			tournamentCareer.activeSeasonInterval.currentEvents.Add(activeEvent);
		}
		Events.EventDefinition @event = seasonEvent.GetEvent();
		activeEvent.stageIndex = Mathf.Clamp((!isWin) ? activeEvent.stageIndex : (activeEvent.stageIndex + 1), 0, @event.tournamentStages.Count);
		bool flag = !isWin || activeEvent.stageIndex >= @event.tournamentStages.Count;
		activeEvent.isStarted = true;
		activeEvent.isFinished = flag;
		activeEvent.eventInfo.score = (int)score;
		if (flag)
		{
			TournamentCareerDAO.GenericEventStats genericEventStats = GetGenericEventStats(seasonEvent.eventDefinitionId);
			if (isWin)
			{
				genericEventStats.numberOfWins++;
			}
			UpdateStatsForNamedEventRecord(seasonEvent.namedEventId, score);
		}
		Save();
	}

	public TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent GetActiveEvent(SeasonDefinitions.NamedEventId eventId)
	{
		foreach (TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent currentEvent in tournamentCareer.activeSeasonInterval.currentEvents)
		{
			if (currentEvent.eventInfo.namedEventId == (int)eventId)
			{
				return currentEvent;
			}
		}
		return null;
	}

	public TournamentCareerDAO.GenericEventStats GetStatsForEvent(SeasonDefinitions.NamedEventId eventId)
	{
		foreach (TournamentCareerDAO.GenericEventStats genericTournamentStat in tournamentCareer.genericTournamentStats)
		{
			if (genericTournamentStat.eventId == (int)eventId)
			{
				return genericTournamentStat;
			}
		}
		return null;
	}

	private void UpdateStatsForNamedEventRecord(SeasonDefinitions.NamedEventId eventId, EventScore score)
	{
		TournamentCareerDAO.NamedEventPerformance namedEventPerformance = GetNamedEventStats(eventId);
		if (namedEventPerformance == null)
		{
			namedEventPerformance = new TournamentCareerDAO.NamedEventPerformance();
			namedEventPerformance.namedEventId = (int)eventId;
			tournamentCareer.namedTournamentPerformances.Add(namedEventPerformance);
		}
		if ((int)score > namedEventPerformance.score)
		{
			namedEventPerformance.score = (int)score;
		}
	}

	private TournamentCareerDAO.GenericEventStats GetGenericEventStats(Events.EventDefinitionId eventId)
	{
		foreach (TournamentCareerDAO.GenericEventStats genericTournamentStat in tournamentCareer.genericTournamentStats)
		{
			if (genericTournamentStat.eventId == (int)eventId)
			{
				return genericTournamentStat;
			}
		}
		TournamentCareerDAO.GenericEventStats genericEventStats = new TournamentCareerDAO.GenericEventStats();
		genericEventStats.eventId = (int)eventId;
		tournamentCareer.genericTournamentStats.Add(genericEventStats);
		return genericEventStats;
	}

	private TournamentCareerDAO.NamedEventPerformance GetNamedEventStats(SeasonDefinitions.NamedEventId eventId)
	{
		foreach (TournamentCareerDAO.NamedEventPerformance namedTournamentPerformance in tournamentCareer.namedTournamentPerformances)
		{
			if (namedTournamentPerformance.namedEventId == (int)eventId)
			{
				return namedTournamentPerformance;
			}
		}
		return null;
	}

	public void Save()
	{
		ProtoIO.SaveToFileCloudSync(tournamentStatsFileName, tournamentCareer);
	}
}
